//
//  HBExpofGoldCtrl.m
//  HuoBao
//
//  Created by sky on 15/12/3.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBExpofGoldCtrl.h"
#import "ExpGoldCell.h"
#import "NetCenter.h"
#import "HBgetUserMoneyListInfo.h"
#import "HBgetUserMoneyListIntemInfo.h"
#import "HBgetUserMoneyListParam.h"
#import "HBgetUserMoneyListParser.h"
#import "HBLoginManager.h"

#import "MJRefresh.h"

@interface HBExpofGoldCtrl ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_table;
}


@property (strong , nonatomic)HBgetUserMoneyListInfo *info;
@property (nonatomic) int pageNum;
@property (strong , nonatomic)HBgetUserMoneyListIntemInfo *iteminfo;

@end

@implementation HBExpofGoldCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)buildLayout{
    self.title = @"体验金明细";
    self.pageNum = 1;
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    INIT_TABLE(_table);
    [self.view addSubview:_table];
    
    __weak UITableView *tableView = _table;
    __weak typeof(self) weakSelf = self;
    
    // 下拉刷新
    tableView.mj_header = [HBOrangeHeader headerWithRefreshingBlock:^{
        
        weakSelf.pageNum = 1;
        [weakSelf RequestFotExpofGold];
        
        if (tableView.mj_footer)
        {
            [tableView.mj_footer resetNoMoreData];
        }
        
        //        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        //        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //            // 结束刷新
        //            [tableView.mj_header endRefreshing];
        //        });
    }];
    
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    tableView.mj_header.automaticallyChangeAlpha = YES;
    
    // 上拉刷新
    tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        //        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        //        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //            // 结束刷新
        //            [tableView.mj_footer endRefreshing];
        //        });
        
        if (weakSelf.info.dataAry.count>0)
        {
            weakSelf.pageNum++;
            [weakSelf RequestFotExpofGold];
        }
        else
        {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                // 结束刷新
                [tableView.mj_footer endRefreshing];
            });
        }
        
    }];
    
//    [_table.mj_header beginRefreshing];
    
    self.pageNum = 1;
    [self RequestFotExpofGold];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma -mark tableViewDelegate




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.info.dataAry.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return 90 + 15;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    ExpGoldCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if (!cell) {
        cell = [[ExpGoldCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"recordsIdentifier"];
    }
    //赋值
    //cell.textLabel.text = [_arr objectAtIndex:indexPath.row];
    cell.iteminfo = self.info.dataAry[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}


-(void)RequestFotExpofGold{
//    [CustomLoadingView showHudToView:nil];
    [CustomLoadingView showHudToView:self.view];
    HBgetUserMoneyListParam *param = [[HBgetUserMoneyListParam alloc]init];
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    param.pageNum = [NSString stringWithFormat:@"%d",self.pageNum];
    HBgetUserMoneyListParser *parser = [[HBgetUserMoneyListParser alloc]init];
    [NetCenter getDataFromURL:Method_getUserMoneyList params:param parser:parser result:^(ParserResult *myResult) {
//        [CustomLoadingView hideHudForView:nil];
        [CustomLoadingView hideHudForView:self.view];
        if (self.pageNum==1)
        {
            [_table.mj_header endRefreshing];
        }
        else
        {
            [_table.mj_footer endRefreshing];
        }
        if (myResult.code==0)
        {
            HBgetUserMoneyListInfo *Info = (HBgetUserMoneyListInfo *)myResult.result;
            if (self.pageNum==1)
            {
                self.info = Info;
                [_table reloadData];
            }
            else
            {
                if (Info.dataAry.count>0)
                {
                    NSMutableArray *array = [NSMutableArray arrayWithArray:self.info.dataAry];
                    [array addObjectsFromArray:Info.dataAry];
                    self.info.dataAry = array;
                    [_table reloadData];
                }
                else
                {
                    self.pageNum--;
                    [_table.mj_footer endRefreshingWithNoMoreData];
                }
            }
            
            
        }
        else
        {
            DISSMISS_ERR(myResult.message);
            self.pageNum--;
        }

    } failure:^(NSError *error) {
//        [CustomLoadingView hideHudForView:nil];
        [CustomLoadingView hideHudForView:self.view];
        DISSMISS_ERR([error description]);
        
        if (self.pageNum==1)
        {
            [_table.mj_header endRefreshing];
        }
        else
        {
            [_table.mj_footer endRefreshing];
            self.pageNum--;
        }
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
